package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.core.utils.StringUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysDictItem;
import com.linq.cool.system.dto.SysDictItemQueryDTO;
import com.linq.cool.system.service.SysDictItemService;
import com.linq.cool.system.service.SysDictService;
import com.linq.cool.system.vo.SysDictItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/23 00:40
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("dict/item")
public class SysDictItemController {
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictItemService sysDictItemService;

    /**
     * 条件分页获取字典元素信息
     *
     * @param dto 字典元素信息
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:dict:list")
    @GetMapping("list")
    public Result<?> list(SysDictItemQueryDTO dto) {
        log.info("SysDictItemQueryDTO: {}", dto);
        IPage<SysDictItemVO> page = sysDictItemService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }


    /**
     * 根据字典元素ID查询信息
     *
     * @param dictItemId 字典元素ID
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:dict:query")
    @GetMapping(value = "{dictItemId}")
    public Result<?> getInfo(@PathVariable("dictItemId") Long dictItemId) {
        return Result.ok(sysDictItemService.getById(dictItemId));
    }

    /**
     * 根据字典编码查询字典数据信息
     *
     * @param dictCode 字典编码
     *
     * @return 结果
     */
    @GetMapping("/code/{dictCode}")
    public Result<?> getDictItems(@PathVariable("dictCode") String dictCode) {
        List<SysDictItem> dictItems = sysDictService.listDictItemsByDictCode(dictCode);
        return Result.ok(StringUtils.isNull(dictItems) ? new ArrayList<SysDictItem>() : dictItems);
    }

    /**
     * 新增字典元素
     *
     * @param dictItem 字典元素
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典元素", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "sys:dict:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysDictItem dictItem) {
        dictItem.setCreator(SecurityUtils.getUserId());
        return sysDictItemService.saveDictItem(dictItem) ? Result.ok() : Result.fail();
    }

    /**
     * 修改保存字典元素信息
     *
     * @param dictItem 字典元素
     *
     * @return 结果
     */
    @Log(moduleTitle = "字典元素", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:dict:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysDictItem dictItem) {
        dictItem.setGmtModified(new Date()).setModifier(SecurityUtils.getUserId());
        return sysDictItemService.updateDictItem(dictItem) ? Result.ok() : Result.fail();
    }

    /**
     * 根据字典数据id删除字典元素
     *
     * @param dictItemIds 字典元素id
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:dict:remove")
    @DeleteMapping("{dictItemIds}")
    public Result<?> remove(@PathVariable("dictItemIds") List<Long> dictItemIds) {
        return sysDictItemService.deleteByIds(dictItemIds) ? Result.ok() : Result.fail();
    }
}
